package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Utils.StringUtil;

import model.bean.CardapioBean;
import model.bean.ClienteBean;

public class CardapioDAO {
	
	public static List<CardapioBean> getCardapios(){
		String sql = "SELECT * FROM CARDAPIO";
		MysqlConnect conn = new MysqlConnect();
		conn.connect();
		ResultSet rs = conn.executeQueryResult(sql);
		List<CardapioBean> cardapios = new ArrayList<>();
		try {
			while(rs.next()){
				String nome = rs.getString("nome");
				String opcoes = rs.getString("opcoes");
				int idCardapio = rs.getInt("idcardapio");
				CardapioBean cardapio = new CardapioBean(nome, opcoes, idCardapio);
				cardapios.add(cardapio);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cardapios;
	}
	public static ArrayList getCardapiosNomes(){
		String sql = "SELECT NOME FROM CARDAPIO";
		MysqlConnect conn = new MysqlConnect();
		conn.connect();
		return conn.consultar(sql);
	}
	public static CardapioBean getCardapioPorNome(String nome){
		MysqlConnect mysql = new MysqlConnect();
		ResultSet rs = mysql.select("CARDAPIO","","NOME = " + StringUtil.quotedStr(nome), "","");
		try {
			while(rs.next()){
				int id = rs.getInt("idcardapio");
				String nomeCardapio = rs.getString("nome");
				String opcoes = rs.getString("opcoes");
				CardapioBean cardapio = new CardapioBean(nomeCardapio, opcoes, id);
				mysql.desconnect();
				return cardapio;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			mysql.desconnect();
		}
		return null;
	}
	public static CardapioBean getCardapioPorId(int id){
			MysqlConnect mysql = new MysqlConnect();
			ResultSet rs = mysql.select("CARDAPIO", "", "idCardapio = " + id, "", "");
			return montaCardapio(rs, mysql);
			
	}
	public static CardapioBean montaCardapio(ResultSet rs, MysqlConnect mysql){
		CardapioBean cardapio = null;
		try {
			while(rs.next()){
				int id = rs.getInt("idcardapio");
				String nomeCardapio = rs.getString("nome");
				String opcoes = rs.getString("opcoes");
				cardapio = new CardapioBean(nomeCardapio, opcoes, id);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		mysql.desconnect();
		return cardapio;
}

}
